//
//  CertDistributionPoint.m
//  TestProject
//
//  Created by 武侠 on 2022/2/18.
//  Copyright © 2022 zhulong. All rights reserved.
//

#import "CertDistributionPoint.h"
#import "CertItemLength.h"

@implementation CertDistributionPoint

- (NSDictionary<NSNumber *,NSString *> *)objectType {
    return @{
        @(0xa0) : @"distributionPoint",
        @(0xa1) : @"reasons",
        @(0xa2) : @"cRLIssuer",
    };
}

/*
 30 33(51):          DistributionPoint
    A0 31(49):          [0]: distributionPoint
        A0 2F(47):           [0]fullName
            86 2D(45): 68 74 74 70 3A 2F 2F 31 31 31 2E 32 30 37 2E 31 37 37 2E 31 38 39 3A 38 30 30 33 2F 63 72 6C 2F 63 61 32 31 63 72 6C 32 39 2E 63 72 6C
 */
- (id)getObject:(NSData *)data type:(uint16_t)type {
    if (type == 0xa0) {
        return [[CertDistributionPointName alloc] initWithData:data];
    } else if (type == 0xa1) {
        return [[CertReasonFlags alloc] initWithData:data];
    } else if (type == 0xa2) {
        return [[CertGeneralName alloc] initWithData:data];
    } else {
        return [super getObject:data type:type];
    }
}

+ (BOOL)isValid:(NSData *)data {
    if (data == nil || data.length == 0) {
        return NO;
    }
    char *sdata = (char *)data.bytes;
    return (uint8_t)sdata[0] == 0x30;
}

- (NSString *)description {
    NSMutableString *str = [NSMutableString string];
    if (self.distributionPoint != nil) {
        NSString *point = [self.distributionPoint.description stringByReplacingOccurrencesOfString:@"\n\t" withString:@"\n\t\t"];
        [str appendFormat:@"distributionPoint:\n\t%@\n", point];
    }
    if (self.reasons != nil) {
        NSString *reasons = [self.reasons.description stringByReplacingOccurrencesOfString:@"\n\t" withString:@"\n\t\t"];
        [str appendFormat:@"reasons:\n\t%@\n", reasons];
    }
    if (self.cRLIssuer != nil) {
        NSString *crl = [self.cRLIssuer.description stringByReplacingOccurrencesOfString:@"\n\t" withString:@"\n\t\t"];
        [str appendFormat:@"cRLIssuer:\n\t%@\n", crl];
    }
    if (str.length > 0) {
        [str deleteCharactersInRange:NSMakeRange(str.length-1, 1)];
    }
    return str;
}

@end


@implementation CertDistributionPoints

- (NSDictionary<NSNumber *,NSString *> *)objectType {
    return @{
        @(0x30) : @"cRLDistributionPoints",
    };
}

- (void)loadCertData {
    self.cRLDistributionPoints = [self getList:self.data type:0x30];
}

/*
 30 81 97(151):
     30 60(96):          distributionPoint
         A0 5E(94):          [0]: distributionPoint
         ....
     30 33(51):          distributionPoint
         A0 31(49):          [0]: distributionPoint
         ....
 */
- (id)getObject:(NSData *)data type:(uint16_t)type {
    if (type == 0x130) {
        return [[CertDistributionPoint alloc] initWithData:data];
    } else {
        return [super getObject:data type:type];
    }
}

+ (BOOL)isValid:(NSData *)data {
    if (data == nil || data.length == 0) {
        return NO;
    }
    char *sdata = (char *)data.bytes;
    return (uint8_t)sdata[0] == 0x30;
}

- (NSString *)description {
    if (self.cRLDistributionPoints.count == 0) {
        return @"无数据";
    }
    NSMutableString *str = [NSMutableString string];
    for (int i = 0; i < self.cRLDistributionPoints.count; i++) {
        NSString *point = [self.cRLDistributionPoints[i].description stringByReplacingOccurrencesOfString:@"\n\t" withString:@"\n\t\t\t"];
        [str appendFormat:@"\t[%d] {\n\t\t%@\n\t}\n", i, point];
    }
    if (str.length > 0) {
        [str insertString:@"[\n" atIndex:0];
        [str deleteCharactersInRange:NSMakeRange(str.length-1, 1)];
        [str appendString:@"\n]"];
    }
    return str;
}

@end
